package org.road0329.dlt698.axdr;

import org.road0329.dlt698.DataUtils;
import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class OAD extends AxdrType {
    private byte[] dataCode = null;
    private Unsigned16 oi = null;
    private OBI obi = null;
    private Unsigned8 index = null;

    public OAD() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setOad(Unsigned16 oi, OBI obi, Unsigned8 index) {
        this.oi = oi;
        this.obi = obi;
        this.index = index;
    }

    public Unsigned16 getOi() {
        if (this.oi != null) {
            return this.oi;
        } else {
            throw new RuntimeException("oi is null");
        }
    }

    public OBI getObi() {
        if (this.obi != null) {
            return this.obi;
        } else {
            throw new RuntimeException("obi is null");
        }
    }

    public Unsigned8 getIndex() {
        if (this.index != null) {
            return this.index;
        } else {
            throw new RuntimeException("index is null");
        }
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.oi = new Unsigned16();
        this.oi.setConst();
        codeLength += this.oi.decode(input);
        this.obi = new OBI();
        this.obi.setNumBit(8);
        codeLength += this.obi.decode(input);
        this.index = new Unsigned8();
        this.index.setConst();
        codeLength += this.index.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            output.writeByte(this.dataCode);
        } else {
            if (this.oi != null && this.obi != null && this.index != null) {
                codeLength = 0;
                codeLength += this.index.encode(output);
                codeLength += this.obi.encode(output);
                codeLength += this.oi.encode(output);
            } else {
                throw new RuntimeException("oi || obi || index is null");
            }
        }
        logEncodeMsg("OAD", output);
        return codeLength;
    }

    @Override
    public String toString() {
        if (this.oi != null && this.obi != null && this.index != null) {
            return "sequence: {" + "oi: " + this.oi + ", obi: " + this.obi + ", index: " + this.index + "}";
        } else {
            return "oi || obi || index is null";
        }
    }

    public String toHexString() throws Exception {
        if (this.dataCode == null) {
            encodeAndSave(4);
        }
        return DataUtils.bytesToHexString(dataCode);
    }
}
